package com.personal.gateway.web;

import com.alibaba.fastjson.JSONObject;
import com.wf.captcha.ArithmeticCaptcha;
import com.personal.gateway.constant.LoginUserInfoConstant;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @program: FlinkGateway
 * @description:
 * @author: yangliao
 * @create: 2021-08-24
 */
@RestController
public class CaptchaController {

    @Resource
    private LoginUserInfoConstant loginUserInfoConstant;

    @RequestMapping("/captcha")
    public JSONObject captcha(ServerHttpRequest request) {
        ArithmeticCaptcha gifCaptcha = new ArithmeticCaptcha(130, 48, 2);
        String text = gifCaptcha.text();

        // 限制每次只能有一人登录， 公司内部使用人少
        String captchaId = UUID.randomUUID().toString();
        loginUserInfoConstant.setCaptcha(captchaId, text);

        return new JSONObject() {
            {
                put("uid", captchaId);
                put("img", gifCaptcha.toBase64());
            }
        };
    }

}
